package com.yuan.model_community.service.impl;

import com.yuan.model_app.rest.resp.UnitUserListResp;
import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityHouse;
import com.yuan.model_community.repository.CommunityBuildingDao;
import com.yuan.model_community.repository.CommunityHouseDao;
import com.yuan.model_community.service.CommunityHouseService;
import com.yuan.model_community.service.dto.CommunityHouseDto;
import com.yuan.model_community.service.mapstruct.CommunityHouseMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@Service
@RequiredArgsConstructor
public class CommunityHouseServiceImpl implements CommunityHouseService {

    private final CommunityHouseDao communityHouseDao;
    private final CommunityHouseMapper communityHouseMapper;

    @Override
    public PageResult<CommunityHouseDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<CommunityHouse> page = communityHouseDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        for (CommunityHouse communityHouse : page) {
            communityHouse.setBuildingName(communityHouseDao.findBuildingNameById(communityHouse.getBuildingId()));
            communityHouse.setUnitName(communityHouseDao.findUnitNameById(communityHouse.getUnitId()));
            communityHouse.setLiverName(communityHouseDao.findUserNameById(communityHouse.getLiverId()));
        }
        return PageUtil.toPage(page.map(communityHouseMapper::toDto));
    }

    @Override
    public List<CommunityHouseDto> queryAll() {
        List<CommunityHouse> all = communityHouseDao.findAll();
        return communityHouseMapper.toDto(all);
    }

    @Override
    public List<CommunityHouseDto> queryAll(CommonQueryCriteria criteria) {
        List<CommunityHouse> users = communityHouseDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return communityHouseMapper.toDto(users);
    }

    @Override
    public void download(List<CommunityHouseDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CommunityHouseDto communityInfoDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CommunityHouse resources) {
        communityHouseDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CommunityHouse resources) throws Exception {
        Optional<CommunityHouse> byId = communityHouseDao.findById(resources.getHouseId());
        if (byId.isPresent()) {
            BeanUtils.copyProperties(resources, byId.get());
            communityHouseDao.save(byId.get());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        communityHouseDao.deleteById(ids);
    }

}
